package com.lcc.book.demos.web.config;

import com.lcc.book.demos.web.intercepter.LoginIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

//项目的配置
//将拦截器注册到项目中
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginIntercepter loginIntercepter;
    private static List<String> excludePath = Arrays.asList("/user/login",
            "/css/*",
            "/js/*",
            "/pic/*",
            "/**/*.html");
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginIntercepter)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
                //下面的方式也可以通过定义一个list来实现
//                .excludePathPatterns("/user/login")
//                .excludePathPatterns("/css/*")
//                .excludePathPatterns("/js/*")
//                .excludePathPatterns("/pic/*")
//                .excludePathPatterns("/**/*.html");//将前端的请求排除拦截
        // addInterceptor(loginIntercepter)表示添加拦截器，.addPathPatterns("/**")表示指定方法，/**指给所有方法都添加拦截器
    }
}
